package com.liuheqiao.sushegaunli;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

public class VideoPlayerActivity extends AppCompatActivity {
    private VideoView videoView;
    private ProgressBar progressBar;
    private ImageButton btnBack;
    private MediaController mediaController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_video_player);

        initViews();
        setupVideoPlayer();
    }

    private void initViews() {
        videoView = findViewById(R.id.videoView);
        progressBar = findViewById(R.id.progressBar);
        btnBack = findViewById(R.id.btn_back);

        // 设置返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());
    }

    private void setupVideoPlayer() {
        // 设置媒体控制器
        mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);

        String videoUrl = getIntent().getStringExtra("video_url");
        Log.d("VideoPlayerActivity", "Video URL: " + videoUrl);

        if (videoUrl != null) {
            try {
                progressBar.setVisibility(View.VISIBLE);
                videoView.setVideoURI(Uri.parse(videoUrl));
                
                // 设置准备完成监听器
                videoView.setOnPreparedListener(this::onVideoPrepared);
                
                // 设置完成监听器
                videoView.setOnCompletionListener(this::onVideoCompleted);
                
                // 设置错误监听器
                videoView.setOnErrorListener(this::onVideoError);
            } catch (Exception e) {
                Log.e("VideoPlayerActivity", "Error setting video", e);
                Toast.makeText(this, "设置视频失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void onVideoPrepared(MediaPlayer mediaPlayer) {
        progressBar.setVisibility(View.GONE);
        mediaPlayer.start();
        // 显示媒体控制器
        mediaController.show(3000);
    }

    private void onVideoCompleted(MediaPlayer mediaPlayer) {
        // 视频播放完成后的处理
        Toast.makeText(this, "视频播放完成", Toast.LENGTH_SHORT).show();
        finish();
    }

    private boolean onVideoError(MediaPlayer mp, int what, int extra) {
        Log.e("VideoPlayerActivity", "Error playing video: what=" + what + " extra=" + extra);
        Toast.makeText(this, "视频播放出错", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 暂停视频
        if (videoView.isPlaying()) {
            videoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放资源
        if (videoView != null) {
            videoView.stopPlayback();
        }
    }

    @Override
    public void onBackPressed() {
        // 处理返回键事件
        if (videoView.isPlaying()) {
            videoView.stopPlayback();
        }
        super.onBackPressed();
    }
} 